package com.ruoyi.common.mybatisplus.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author zhengcl
 */
@Slf4j
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    String username = SecurityUtils.getUsername();

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        log.info("username insert fill："+ username);
        // 或者
        this.strictInsertFill(metaObject, "createBy", String.class, username); // 起始版本 3.3.3(推荐)
        this.strictInsertFill(metaObject, "createTime", Date::new,  Date.class); // 起始版本 3.3.3(推荐)
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        log.info("username update fill："+ username);
        this.strictInsertFill(metaObject, "updateBy", String.class, username); // 起始版本 3.3.3(推荐)
        this.strictUpdateFill(metaObject, "updateTime", Date::new,  Date.class); // 起始版本 3.3.0(推荐)
    }
}
